package com.zs.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.blog.constant.enums.ResponseResultEnum;
import com.zs.blog.converter.TagConverter;
import com.zs.blog.dao.ITagMapper;
import com.zs.blog.exception.BusinessException;
import com.zs.blog.model.ResponseResult;
import com.zs.blog.model.dto.TagDTO;
import com.zs.blog.model.dto.TagEditDTO;
import com.zs.blog.model.dto.TagSearchDTO;
import com.zs.blog.model.entity.Tag;
import com.zs.blog.model.vo.*;
import com.zs.blog.service.ITagService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Title: ITagServiceImpl
 * @Author zs
 * @Package com.zs.blog.service.impl
 * @Date 2024/10/13 21:19
 * @description: 标签服务层实现
 */
@Service
public class ITagServiceImpl extends ServiceImpl<ITagMapper, Tag> implements ITagService {

    public ITagMapper tagMapper;

    @Autowired
    public ITagServiceImpl(ITagMapper tagMapper) {
        this.tagMapper = tagMapper;
    }

    /**
     * 获取标签分页列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return
     */
    @Override
    public PageInfoVO<TagListVO> listTagWithPagination(Integer pageNum, Integer pageSize) {
        // 获取标签分页列表
        Page<TagDTO> page = new Page<>(pageNum, pageSize);
        List<TagDTO> tagDtoList = tagMapper.listTagWithPagination(page);
        System.out.println(tagDtoList);

        // 属性值映射
        List<TagListVO> tagListVos = TagConverter.INSTANCE.tagDTO2TagListVO(tagDtoList);

        // 数据整理
        PageInfoVO<TagListVO> pageInfoVo = new PageInfoVO<>();
        pageInfoVo.setTotal(page.getTotal());
        pageInfoVo.setItems(tagListVos);

        return pageInfoVo;
    }

    /**
     * 根据查询条件获取标签分页列表
     *
     * @param tagSearchDTO
     * @return
     */
    @Override
    public PageInfoVO<TagListVO> listTagBySearchCondition(TagSearchDTO tagSearchDTO) {
        // 根据搜索条件获取标签分页列表
        Page<TagDTO> page = new Page<>(tagSearchDTO.getPageNum(), tagSearchDTO.getPageSize());
        LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotBlank(tagSearchDTO.getTagName()), Tag::getTagName, tagSearchDTO.getTagName())
                .eq(tagSearchDTO.getIsDeleted() != null&&StringUtils.isNotBlank(tagSearchDTO.getIsDeleted().toString()), Tag::getIsDeleted, tagSearchDTO.getIsDeleted());
        if (StringUtils.isNotBlank(tagSearchDTO.getStartTime().toString()) && StringUtils.isNotBlank(tagSearchDTO.getEndTime().toString())) {
            lqw.between(Tag::getCreateTime, tagSearchDTO.getStartTime(), tagSearchDTO.getEndTime());
        }
        List<TagDTO> tagDTOList =  tagMapper.listTagBySearchCondition(page, lqw);

        // 属性值映射
        List<TagListVO> tagListVOS = TagConverter.INSTANCE.tagDTO2TagListVO(tagDTOList);

        // 数据整理
        PageInfoVO<TagListVO> pageInfoVO = new PageInfoVO<>();
        pageInfoVO.setTotal(page.getTotal());
        pageInfoVO.setItems(tagListVOS);

        return pageInfoVO;
    }

    /**
     * 根据标签ID列表删除标签信息
     * @param tagIdList 标签ID列表
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<?> deleteTagByIds(List<Integer> tagIdList) {
        // 标签下有文章,则不能删除
        Integer count = tagMapper.getArticleCountUnderTag(tagIdList);
        if (count > 0){
            return ResponseResult.failure(ResponseResultEnum.TAG_EXIT_ARTICLE);
        }

        tagMapper.deleteTagByIds(tagIdList);
        return ResponseResult.success();
    }

    /**
     *  新增或更新标签信息
     * @param tagEditDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdateTag(TagEditDTO tagEditDTO) {
        // 判断标签名称是否存在
        LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Tag::getTagName,tagEditDTO.getTagName());
        Tag tag = tagMapper.selectOne(lqw);
        if (Objects.nonNull(tag)){
            throw new BusinessException("标签名称已存在");
        }

        // 新增或更新标签
        Tag t = TagConverter.INSTANCE.tagEditDTO2Tag(tagEditDTO);
        tagMapper.insertOrUpdate(t);
    }

    /**
     * 根据标签ID获取标签信息
     * @param tagId 标签ID
     * @return
     */
    @Override
    public TagEditVO getTagByTagId(Long tagId) {
        LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Tag::getId,tagId);
        Tag tag = tagMapper.selectOne(lqw);

        return  TagConverter.INSTANCE.tag2TagEditVO(tag);
    }

    /**
     * 查询所有标签信息
     * @return
     */
    @Override
    public List<TagListVO> listTag() {
        return tagMapper.listTag();
    }
}
